package com.hs.dao.DaoImpl;

import com.hs.dao.GoodsDao;
import com.hs.entity.Community;
import com.hs.entity.Goods;
import com.hs.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public List<Goods> selectAll(Map<String, String> map) {
        List<Goods> list = new ArrayList<>();
        String sql = "select * from goods where 1=1 ";
        List params = new ArrayList();
        String goodsName = map.get("goodsName");
        if (goodsName != null && !"".equals(goodsName)) {
            sql += " and goodsName like ?";
            params.add("%" + goodsName + "%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String goodsShopName = map.get("shopName");
        if (goodsShopName != null && !"".equals(goodsShopName)) {
            sql += " and goodsShopName like ?";
            params.add(goodsShopName);
        }

        String goodsStatus = map.get("goodsStatus");
        if ("1".equals(goodsStatus)) {
            sql += " and goodsStatus = ?";
            params.add(goodsStatus);
        } else if ("0".equals(goodsStatus)) {
            sql += " and goodsStatus = ?";
            params.add(goodsStatus);
        }
        //判断是否分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,?";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }

        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                //组成商品对象
                Goods goods = new Goods(
                        res.getInt(1),
                        res.getString(2),
                        res.getDate(3),
                        res.getString(4),
                        res.getDouble(5),
                        res.getInt(6),
                        res.getString(7),
                        res.getInt(8),
                        res.getString(9),
                        res.getInt(10),
                        res.getInt(11)
                );
                list.add(goods);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();


        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        String sql = "select count(1) from goods where 1=1 ";
        List params = new ArrayList();
        String goodsName = map.get("goodsName");
        if (goodsName != null && !"".equals(goodsName)) {
            sql += " and goodsName like ?";
            params.add(goodsName);
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }

        String goodsShopName = map.get("shopName");
        if (goodsShopName != null && !"".equals(goodsShopName)) {
            sql += " and goodsShopName like ?";
            params.add(goodsShopName);
        }

        String goodsStatus = map.get("goodsStatus");
        if ("1".equals(goodsStatus)) {
            sql += " and goodsStatus = ?";
            params.add(goodsStatus);
        } else if ("0".equals(goodsStatus)) {
            sql += " and goodsStatus = ?";
            params.add(goodsStatus);
        }

        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                count = res.getInt(1);

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return count;
    }

    @Override
    public int  changeStatus(String goodsStatus,Integer goodsId) {
        String sql = "update goods set goodsStatus = ? where goodsId = ?";
//        Object obj[] = {"0",goodsId};
//        Object[] obj = new Object[2];
        int num = 0;
        if ("1".equals(goodsStatus)) {
            Object[] obj = {"0",goodsId};
           num = JDBCDruidUtil.update(sql, obj);
        } else if ("0".equals(goodsStatus)) {
            Object[] obj = {"1",goodsId};
            num = JDBCDruidUtil.update(sql, obj);
        }

//        Object[] obj = param.toArray();

//        return JDBCDruidUtil.update(sql, obj);
        return  num;
    }
}
